package com.yuke.cloud.service.smc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.uac.dto.UserInfoDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.smc.dto.DmsScopeDto;
import com.yuke.cloud.service.smc.entity.DmsScope;
import com.yuke.cloud.service.smc.service.DmsScopeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by zn on 2020/07/03.
*/
@Api(value = "WEB - DmsScopeController", tags = "处理DmsScope相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/dms/scope")
public class DmsScopeController extends BaseController {
    @Resource
    private DmsScopeService dmsScopeService;
    @Resource
    private UacUserFeignApi uacUserFeignApi;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "dmsScope", value = "该数据记录的POJO") @RequestBody DmsScope dmsScope) {
        dmsScopeService.save(dmsScope);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        dmsScopeService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "dmsScope", value = "需要修改的内容") @RequestBody DmsScope dmsScope) {
        dmsScopeService.update(dmsScope);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<DmsScope> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        DmsScope dmsScope = dmsScopeService.findById(id);
        return WrapMapper.ok(dmsScope);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<DmsScopeDto>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                               @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<DmsScope> list = dmsScopeService.findAll();
        List<DmsScopeDto> dmsScopeDtoList = new ArrayList<>();

        if (list != null && list.size() > 0 ){
            //获取userId
            List<Long> userIdList = list.stream().map(DmsScope::getUserId).collect(Collectors.toList());
            Wrapper<List<UserInfoDto> > userInfoDtos = uacUserFeignApi.getUserInfoByUserIds(userIdList);
            if (userInfoDtos.getCode() == 200){
                List<UserInfoDto> userInfoDtoList = userInfoDtos.getResult();
                for(DmsScope item : list){
                    for(UserInfoDto userInfoDto : userInfoDtoList){
                        if (item.getUserId().equals(userInfoDto.getUserId())){
                            DmsScopeDto dmsScopeDto = new DmsScopeDto();
                            BeanUtils.copyProperties(item,dmsScopeDto);
                            dmsScopeDto.setUserName(userInfoDto.getUserName());
                            dmsScopeDtoList.add(dmsScopeDto);
                        }
                    }
                }
            }
        }
        PageInfo pageInfo = new PageInfo(dmsScopeDtoList);
        return WrapMapper.ok(pageInfo);
    }

    @GetMapping("/getScopeList")
    @ApiOperation(httpMethod = "GET", value = "获取所有记录")
    public Wrapper<List<DmsScope>> list() {
        List<DmsScope> list = dmsScopeService.findAll();
        return WrapMapper.ok(list);
    }
}
